package ch.uzh.ifi.seal.ssqa.model.diagram.navigator;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import ch.uzh.ifi.seal.ssqa.model.Diagram;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramComposite2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeName2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeNameEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolder2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderName2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderNameEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModule2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleName2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleNameEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramVComEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaDiagramEditorPlugin;
import ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaVisualIDRegistry;
import ch.uzh.ifi.seal.ssqa.model.diagram.providers.SsqaElementTypes;
import ch.uzh.ifi.seal.ssqa.model.diagram.providers.SsqaParserProvider;

/**
 * @generated
 */
public class SsqaNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		SsqaDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		SsqaDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof SsqaNavigatorItem
				&& !isOwnView(((SsqaNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof SsqaNavigatorGroup) {
			SsqaNavigatorGroup group = (SsqaNavigatorGroup) element;
			return SsqaDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof SsqaNavigatorItem) {
			SsqaNavigatorItem navigatorItem = (SsqaNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (SsqaVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http:///ch/uzh/ifi/seal/ssqa/model.ecore?Diagram", SsqaElementTypes.Diagram_1000); //$NON-NLS-1$
		case DiagramFolderEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramFolder", SsqaElementTypes.DiagramFolder_2001); //$NON-NLS-1$
		case DiagramFolder2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramFolder", SsqaElementTypes.DiagramFolder_3001); //$NON-NLS-1$
		case DiagramVComEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramVCom", SsqaElementTypes.DiagramVCom_4001); //$NON-NLS-1$
		case DiagramModule2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramModule", SsqaElementTypes.DiagramModule_3002); //$NON-NLS-1$
		case DiagramComposite2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramComposite", SsqaElementTypes.DiagramComposite_3003); //$NON-NLS-1$
		case DiagramCompositeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramComposite", SsqaElementTypes.DiagramComposite_2003); //$NON-NLS-1$
		case DiagramModuleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///ch/uzh/ifi/seal/ssqa/model.ecore?DiagramModule", SsqaElementTypes.DiagramModule_2002); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = SsqaDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& SsqaElementTypes.isKnownElementType(elementType)) {
			image = SsqaElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof SsqaNavigatorGroup) {
			SsqaNavigatorGroup group = (SsqaNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof SsqaNavigatorItem) {
			SsqaNavigatorItem navigatorItem = (SsqaNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (SsqaVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getDiagram_1000Text(view);
		case DiagramFolderEditPart.VISUAL_ID:
			return getDiagramFolder_2001Text(view);
		case DiagramFolder2EditPart.VISUAL_ID:
			return getDiagramFolder_3001Text(view);
		case DiagramVComEditPart.VISUAL_ID:
			return getDiagramVCom_4001Text(view);
		case DiagramModule2EditPart.VISUAL_ID:
			return getDiagramModule_3002Text(view);
		case DiagramComposite2EditPart.VISUAL_ID:
			return getDiagramComposite_3003Text(view);
		case DiagramCompositeEditPart.VISUAL_ID:
			return getDiagramComposite_2003Text(view);
		case DiagramModuleEditPart.VISUAL_ID:
			return getDiagramModule_2002Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getDiagramModule_2002Text(View view) {
		IParser parser = SsqaParserProvider.getParser(
				SsqaElementTypes.DiagramModule_2002,
				view.getElement() != null ? view.getElement() : view,
				SsqaVisualIDRegistry
						.getType(DiagramModuleNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDiagramComposite_3003Text(View view) {
		IParser parser = SsqaParserProvider.getParser(
				SsqaElementTypes.DiagramComposite_3003,
				view.getElement() != null ? view.getElement() : view,
				SsqaVisualIDRegistry
						.getType(DiagramCompositeName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDiagramVCom_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getDiagram_1000Text(View view) {
		Diagram domainModelElement = (Diagram) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getIdentifier();
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDiagramFolder_2001Text(View view) {
		IParser parser = SsqaParserProvider.getParser(
				SsqaElementTypes.DiagramFolder_2001,
				view.getElement() != null ? view.getElement() : view,
				SsqaVisualIDRegistry
						.getType(DiagramFolderNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDiagramFolder_3001Text(View view) {
		IParser parser = SsqaParserProvider.getParser(
				SsqaElementTypes.DiagramFolder_3001,
				view.getElement() != null ? view.getElement() : view,
				SsqaVisualIDRegistry
						.getType(DiagramFolderName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDiagramComposite_2003Text(View view) {
		IParser parser = SsqaParserProvider.getParser(
				SsqaElementTypes.DiagramComposite_2003,
				view.getElement() != null ? view.getElement() : view,
				SsqaVisualIDRegistry
						.getType(DiagramCompositeNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDiagramModule_3002Text(View view) {
		IParser parser = SsqaParserProvider.getParser(
				SsqaElementTypes.DiagramModule_3002,
				view.getElement() != null ? view.getElement() : view,
				SsqaVisualIDRegistry
						.getType(DiagramModuleName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SsqaDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return DiagramEditPart.MODEL_ID.equals(SsqaVisualIDRegistry
				.getModelID(view));
	}

}
